﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VM.BLL.System.Code;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Entity.EntityDto.EnTry;
using VM.Entity.EntityDto.System.Code;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.EnTry;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.System.Entry;
using VM.IBLL.Topic;
using VM.IDAL.System.Entry;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{


    /// <summary>
    /// 数据字典相关控制器
    /// </summary>
    [Route("api/Entry")]
    [ApiController]
    public class EntryController : ControllerBase
    {
        private readonly IEntryBLL _IEntryBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;

        public EntryController(IJWTService iJWTService, IEntryBLL iEntryBLL, IHttpContextAccessor httpContextAccessor)
        {

            _IEntryBLL = iEntryBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 添加词条
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addInsterestEntry")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addInsterestEntry(addEntryDto obj)
        { 
            //解析Jwt,获取到UserId
            int UserId= _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj= _IEntryBLL.perfectClass<addEntryDto>(obj,UserId,ProTypeEnum.save);
            return _IEntryBLL.insertEntry(obj);
        } 
        /// <summary>
        /// 更新词条
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("updateInsterestEntry")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult updateInsterestEntry(updateCodeRuleDto obj)
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj = _IEntryBLL.perfectClass<updateCodeRuleDto>(obj, UserId, ProTypeEnum.update);
            return _IEntryBLL.updateEntry(obj);
        }
      
        /// <summary>
        /// 获取词条树形列表，   PHONE
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getInsterestEntryTreeList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<List<getEntryTreeListDto>> getInsterestEntryTreeList(string? name = "")
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();

            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<List<getEntryTreeListDto>> Res = _IEntryBLL.getEntryTreeList(name, UserId);
            return Res;
        }


        /// <summary>
        /// 添加用户标签关联
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addUserEntryLink")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addUserEntryLink(string entryIds)
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
             
            return _IEntryBLL.addUserEntryLink(UserId,entryIds);
        }



        ///// <summary>
        ///// 获取编码规则列表，   PC
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //[Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        //[Route("getEntryList")]
        //[ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        //public ApiDataResult<PagingData<getEntryTreeListDto>> getEntryList(string? name = "", int pageIndex = 1, int pageSize = 100)
        //{
        //    //获取到header 内的用户信息进行解析
        //    var authorizationHeader = _HttpContextAccessor.HttpContext.Request.Headers["Authorization"];
        //    //解析jwt
        //    string tokenInfo = authorizationHeader.ToString().Replace("bearer ", "").Replace("Bearer ", "");
        //    var AESId = _IJWTService.AnaliseToken(tokenInfo, "id");
        //    //解析userid
        //    int UserId = Convert.ToInt32(AESUntil.Decrypt(AESId));
        //    //校验验证码是否可用，如果可用，返回true
        //    ApiDataResult<PagingData<getEntryTreeListDto>> Res = _IEntryBLL.get(name, UserId, pageSize, pageIndex);
        //    return Res;
        //}



    }
}
